package domain;

import org.hibernate.Session;
import util.HibernateUtil;
public class DAOAddress {
	Session session = null;
	private static DAOAddress dao=null;
	
	public DAOAddress(){		
		session = HibernateUtil.getSessionFactory().openSession();
	}
	
	
	public static DAOAddress getInstance(){
		if(dao==null)
			dao=new DAOAddress();
		return dao;
	}
	
	
	public Address addAddress(String street,String city,String zip,String country){
		try{
			//begin a transaction
			org.hibernate.Transaction tx = session.beginTransaction();
			//create a contact and save it into the DB
			Address address=new Address();
			address.setStreet(street);
			address.setCountry(country);
			address.setZip(zip);
			address.setCity(city);

			//save the contact into the DB
			session.save(address); // or session.persist(contact);


			//mandatory to flush the data into the DB
			tx.commit();
			return address;
			}catch(Exception e){
						System.out.println(e.getMessage());
			}
			return null;
	}
	
	public boolean modifyAdress(long id, String street,String city,String zip,String country){
		boolean success = false;
		Address address=null;
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			address=(Address) session.get(Address.class, id);
			address.setStreet(street);
			address.setCountry(country);
			address.setZip(zip);
			address.setCity(city);
			success = true;
			tx.commit();
		} catch( Exception e ){
			e.printStackTrace();
		}
		return success;		
	}
	
}
